/**
 * Created by 功振 on 2016/2/3.
 */
angular.module('NatureCloud').controller('BuildUpdateController', ["$scope", "$http", "$uibModalInstance", "$cookies", "buildUpdate", function ($scope, $http, $uibModalInstance, $cookies, buildUpdate) {
    $scope.operation = "更新";
    $scope.disabled = true;
    $scope.isRepoListShow = false;
    $scope.isRepoInfoShow = true;
    var buildInfo = $scope.updateBuildInfo;
    $scope.id = buildInfo.id;
    $scope.gitPath = buildInfo.gitPath;
    $scope.user = buildInfo.user;
    $scope.buildContext = buildInfo.buildContext;
    $scope.repoName = buildInfo.repoName;

    $scope.buildName = buildInfo.id;

    $scope.isCI = buildInfo.isCI;


    $scope.ok = function () {
        var user = $cookies.get("username");
        $scope.updateBuildInfo.buildContext = $scope.buildContext === undefined ? '' : $scope.buildContext;
        $scope.updateBuildInfo.repoName = buildInfo.id;
        $scope.updateBuildInfo.isCI = ($scope.isCI === undefined) ? buildInfo.isCI : $scope.isCI;


        buildUpdate(angular.toJson($scope.updateBuildInfo, true)).then(function (r) {
            var data = r.data.data;
            if(data.n === 1 && data.nModified === 1 && data.ok === 1){
                alertify.success("build update succeed");
                $uibModalInstance.close(data.jobInfo);
            }else{
                alertify.error("数据库更新失败");
                $uibModalInstance.close();
            }
        }, function (r) {

            alertify.error("build update failed");
            $uibModalInstance.close();
        });
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
}]);